<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Facades\DB;


class UserModel extends Model
{
    // 獲取首頁導航
    // static public function getNav(){
    //     return DB::table('article_category')->select('id','type_name','type_url')->where(['is_hidden'=>0,'pid'=>0])->orderBy('sort_order','asc')->get();
    // }
    // 獲取地區列表
    static public function getAreaList($map = array()){
        return DB::table('area')->where($map)->orderBy('sort_order','asc')->get();
    }
    // 獲取網站參數
    static public function getGlobal(){
        return DB::table('global')->orderBy('sort_order','desc')->get();
    }
    // 獲取網站參數的值
    static public function getGlobalVal(){
        $global = DB::table('global')->orderBy('sort_order','desc')->get();
        $res = array();
        // dd($global);exit;
        foreach($global as $v){
            $res[$v->code] = $v->text;
        }
        return $res;
    }
    // 獲取分類
    static public function getCate($table,$map = array()){
        if(!empty($map)){
            return DB::table($table)->where($map)->orderBy('id','asc')->get();
        }else{
            return DB::table($table)->orderBy('id','asc')->get();
        }
        
    }
    // 獲取壹個用戶
    static public function getOneUser($map){
        return DB::table(DB::raw('zzb_user u'))->leftJoin(DB::raw('zzb_user_money m'), DB::raw('u.id'), '=', DB::raw('m.uid'))->where($map)->first();
    }
    // 獲取壹個用戶的資金詳情
    static public function getMoney($uid){
        return DB::table('user_money')->where('uid','=',$uid)->first();
    }
    // 獲取壹個訂單
    static public function getOneOrder($id){
        return DB::table(DB::raw('zzb_order o'))->select(DB::raw('o.id,o.num,o.phone,o.phone,o.add_time,o.money,o.order,o.status,p.name,p.weight,p.price'))->leftJoin(DB::raw('zzb_product p'), DB::raw('p.id'), '=', DB::raw('o.pid'))->where(DB::raw('o.id'),'=',DB::raw($id))->first();
    }
    // 獲取訂單列表
    static public function getOrderList($uid,$type=3,$count=4){
        if($type>=0 && $type<=2){
            return DB::table(DB::raw('zzb_order o'))->select(DB::raw('o.id,o.num,o.phone,o.phone,o.add_time,o.money,o.order,o.status,o.is_show,p.name,p.weight,p.price'))->leftJoin(DB::raw('zzb_product p'), DB::raw('p.id'), '=', DB::raw('o.pid'))->where(DB::raw('o.uid'),'=',DB::raw($uid))->where(DB::raw('o.status'),'=',DB::raw($type))->orderBy(DB::raw('o.id'),'asc')->paginate($count);
        }else{
            return DB::table(DB::raw('zzb_order o'))->select(DB::raw('o.id,o.num,o.phone,o.phone,o.add_time,o.money,o.order,o.status,o.is_show,p.name,p.weight,p.price'))->leftJoin(DB::raw('zzb_product p'), DB::raw('p.id'), '=', DB::raw('o.pid'))->where(DB::raw('o.uid'),'=',DB::raw($uid))->orderBy(DB::raw('o.id'),'asc')->paginate($count);
        }
        
    }
    // 根據UID更新用戶信息
    static public function updateUser($id,$data){
        return DB::table('user')->where('id','=',$id)->update($data);
    }
    

}
